package com.work.saturn.main;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.immo.libcommon.base.BaseActivity;
import com.immo.libcommon.utils.ToastUtils;
import com.immo.libcommon.utils.ViewPagerAdapter;
import com.immo.libcommon.view.TabLayoutView;
import com.work.saturn.R;
import com.immo.libcommon.constant.RouterUtil;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author YaoChen
 * @content 首页
 * @date 2018/07/16
 */
@Route(path = RouterUtil.MAIN_PAGE)
public class MainActivity extends BaseActivity {

    @BindView(R.id.main_viewpager)
    ViewPager viewPager;
    @BindView(R.id.main_tab)
    TabLayoutView tabLayoutView;
    //退出时间
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragments();
        initTab();
    }

    /**
     * 初始化底部tab
     */
    private void initTab() {
        //底部tab栏文字
        String[] titles = new String[]{"资产",
                "好玩", "我的"};
        //未选中时的图片
        int[] imgs = {R.mipmap.tab_1, R.mipmap.tab_3, R.mipmap
                .tab_4};
        //选中时的图片
        int[] imgsselect = {R.mipmap.tab1, R.mipmap.tab3, R.mipmap
                .tab4};
        //设置数据
        tabLayoutView.setDataSource(titles, imgs, imgsselect, 0);
        //设置图片宽高
        tabLayoutView.setImageStyle(25, 25);

        //设置文字样式
        tabLayoutView.setTextStyle(12, R.color.colorBlack, R.color.colorYellow);
        //初始化数据
        tabLayoutView.initDatas();
        //设置tab的点击监听时间
        tabLayoutView.setOnItemOnclickListener(index -> viewPager.setCurrentItem(index, false));
        //设置页面改动时事件
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                //viewPager设置选中
                viewPager.setCurrentItem(position, false);
                //tab设置选中
                tabLayoutView.setSelectStyle(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 初始化fragment
     */
    private void initFragments() {
        List<Fragment> fragmentPages = new ArrayList<>();
        //资产
        fragmentPages.add((Fragment) ARouter.getInstance().build(RouterUtil.ASSETS_FRAGMENT).navigation());
//        //行情
//        fragmentPages.add((Fragment) ARouter.getInstance().build(RouterUtil.QUOTATION_FRAGMENT).navigation());
        //好玩
        fragmentPages.add((Fragment) ARouter.getInstance().build(RouterUtil.INTER_FRAGMENT).navigation());
        //我的
        fragmentPages.add((Fragment) ARouter.getInstance().build(RouterUtil.MINE_FRAGMENT).navigation());
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), fragmentPages));
        //页面预加载
        viewPager.setOffscreenPageLimit(3);
    }

    /**
     * 返回按钮的回退
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //按下返回的按钮时
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //System.currentTimeMillis()无论何时调用，肯定大于2000,连续按返回按钮时间小于2s,就退出应用
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtils.showToast(getApplicationContext(), getString(R.string.press_to_out));
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
